<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 200px;
            height: 200px;

            position: absolute;
            top: 0;
            left: 0;
        }
        img{
            width: 100%;
            height: 100%;
            display: block;
        }
    </style>
</head>
<body>
    <div>
        <img src="https://img2.baidu.com/it/u=1007704835,3947912866&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" alt="">
    </div>
    <script>
        /* 美女跟随鼠标移动 
            1. 鼠标移动事件
                获取鼠标移动的坐标
                让盒子跟随鼠标移动
            2. 让鼠标在盒子的最中间
                获取盒子宽高 给盒子赋值的是 x 减掉盒子的一般,y要剪掉盒子的一般
        */
        var div = document.querySelector("div")
        document.onmousemove = function(e){
            //处理兼容性
            e = e || window.event
            //获取鼠标移动的坐标
            var x = e.clientX
            var y = e.clientY

            // 获取盒子的宽高
            var boxWidht = div.offsetWidth
            var boxHeight = div.offsetHeight

            // 盒子最终的坐标
            var endy = y - boxHeight / 2
            var endx =  x - boxWidht / 2

            //临界值判断
            if(endx < 100){
                endx = 0;
            }
            if(endy < 100){
                endy = -190;
            }
            if(endx > document.documentElement.clientWidth - boxWidht){
                endx = document.documentElement.clientWidth - boxWidht
            }
            if(endy > document.documentElement.clientHeight - boxHeight){
                endy = document.documentElement.clientHeight - boxHeight
            }

            //让div跟随鼠标移动
            div.style.top = endy + 'px'
            div.style.left = endx + 'px'
        }
    </script>
</body>
</html>